package com.example.himalaya2.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya2.R;
import com.example.himalaya2.base.BaseApplication;
import com.example.himalaya2.views.SobPopWindow;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.InnerHolder> {
    private List<Track> mData = new ArrayList<>();
    private int playingIndex = 0;
    private SobPopWindow.PlayListItemClickedListener mItemClickListener ;

    @NonNull
    @NotNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {  // 设置布局
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list,parent,false);
        return new InnerHolder(itemView);
    }

    @Override // 设置数据
    public void onBindViewHolder(@NonNull @NotNull PlayListAdapter.InnerHolder holder, int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClicked(position);
                }
            }
        });
        Track track = mData.get(position);
        TextView title = holder.itemView.findViewById(R.id.track_title_tv);
        if (playingIndex == position) {
            title.getPaint().setFakeBoldText(true);
        }else {
            title.getPaint().setFakeBoldText(false);
        }
        title.setTextColor(playingIndex == position?BaseApplication.getAppContext().getResources().getColor(R.color.play_list_playing_text):
                BaseApplication.getAppContext().getResources().getColor(R.color.play_list_text_color));
        title.setText(track.getTrackTitle());
        // 找播状态的图标

        View playingIconView = holder.itemView.findViewById(R.id.play_icon_iv);
        playingIconView.setVisibility(playingIndex== position?View.VISIBLE:View.GONE);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<Track> tracks) {
        // 设置数据，更新列表
        this.mData.clear();
        this.mData.addAll(tracks);
        notifyDataSetChanged();
    }

    public void setSetCurrentPlayPosition(int position) {
        playingIndex = position;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(SobPopWindow.PlayListItemClickedListener listener) {
        this.mItemClickListener = listener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder{
        public InnerHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }
    }
}
